package com.example.redismanual.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import org.assertj.core.util.Arrays;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;

@SpringBootTest
public class RedisListDao {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Test
    public void testListOps() {
        stringRedisTemplate.delete("listKey");
        ListOperations<String, String> listClient = stringRedisTemplate.opsForList();
        listClient.leftPush("listKey", "one");
        listClient.leftPush("listKey", "two");
        listClient.leftPush("listKey", "three");
        // assert "three".equals(listClient.leftPop("listKey"));
        listClient.leftPush("listKey", "three");
        // assert "one".equals(listClient.rightPop("listKey"));
        listClient.rightPush("listKey", "one");
        List<String> numList = new ArrayList<>();
        numList.add("four");
        numList.add("five");
        numList.add("six");
        listClient.leftPushAll("listKey", numList);

        // assert "six".equals(listClient.leftPop("listKey"));
        listClient.rightPopAndLeftPush("listKey", "listKey");
        // assert "one".equals(listClient.leftPop("listKey"));
        // assert listClient.size("listKey") == 4;
        System.out.println(listClient.indexOf("listKey", "five") );
        // assert listClient.indexOf("listKey", "five") == 0;
        System.out.println(listClient.index("listKey", 1));
        // assert "five".equals(listClient.index("listKey", 0));
    }

    @Test
    public void testConvert(){
        List<String> strings = new ArrayList<>();
        strings.add("test1");
        strings.add("test2");
        strings.add("test3");
        strings.add("test4");
        String listString = strings.toString();
        System.out.println(listString);
        Map<String,String> testMap = new HashMap<>();
        testMap.put("key", "value");
        testMap.put("key2", "value");
        testMap.put("key3", "value");

        List<Map<String,String>> mapList = new ArrayList<>();
        mapList.add(testMap);
        mapList.add(testMap);
        String mapListString = mapList.toString();
        System.out.println(mapList.toString());
        System.out.println(JSONArray.parseArray(mapListString, List.class));

    }
}
